package com.web.boot.web.base;

import com.blue.core.dict.HttpMethod;
import com.blue.core.dict.annotation.DictParser;
import com.blue.http.annotation.Http;
import com.blue.http.message.HttpRequest;
import com.blue.http.message.View;
import com.blue.jdbc.Page;
import com.blue.validation.group.SaveModel;
import com.web.core.base.model.Sequence;
import com.web.core.base.service.SequenceService;
import com.web.core.util.Const;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;

@Controller
@Http(url = "/base")
public class SequenceController
{
	@Resource
	private SequenceService sequenceService;
	
	private DictParser dictParser = DictParser.getInstance();
	
	public SequenceController()
	{
	}
	
	@Http(url = "/sequence_list", method = HttpMethod.GET)
	public View listGet(HttpRequest request)
	{
		Sequence sequence = request.getQueryObject(Sequence.class);
		return View.createBuilder().setView("/base/sequence_list")
				.put("page", sequenceService.list(sequence, null))
				.put("model", sequence)
				.put("map", dictParser.getStringMap())
				.build();
	}
	
	@Http(url = "/sequence_list", method = HttpMethod.POST)
	public View listPost(HttpRequest request)
	{
		Sequence sequence = request.getPostObject(Sequence.class);
		Page<Sequence> page = new Page<>(Const.PAGE_SIZE);
		page.setCurPage(request.getPostInt("start", Const.PAGE_START));
		return View.createBuilder().setView("/base/sequence_list_grid")
				.put("page", sequenceService.list(sequence, page))
				.build();
	}
	
	@Http(url = "/sequence_get", method = HttpMethod.GET)
	public View get(HttpRequest request)
	{
		Sequence sequence = request.getQueryObject(Sequence.class);
		if (sequence.getId() != null)
		{
			sequence = sequenceService.get(sequence.getId());
		}
		return View.createBuilder().setView("/base/sequence_get")
				.put("model", sequence)
				.put("map", dictParser.getStringMap())
				.build();
	}
	
	@Http(url = "/sequence_save", method = HttpMethod.POST)
	public void save(HttpRequest request)
	{
		Sequence sequence = request.getPostObject(Sequence.class, SaveModel.class);
		sequenceService.save(sequence);
	}
	
}
